<script>
	function showCountersign(audit_type){
		if(audit_type == '2'){//2指会签审核
			$('.countersign').each(function(){
				$(this).show('slow');
			});
		}else{
			$('.countersign').each(function(){
				$(this).hide('slow');
			});
		}
	}
	//非空判断
	 function isBlank(atrr, name,tips) {
		if (atrr == null || atrr == "") {
		 $('#'+tips).text(name + "不能为空");
			return false;
		  } else{
		     $('#'+tips).text("");
		     return true;
		  }
		}
		
	checkIfExist = function(category, itemName){
		var ret = false;
		if(itemName != null && itemName != ''){ 
		$.ajax({
			url: '/crm/dataDict/isExist', 
			type: "post",
			async : false,
			data : {itemName:itemName, category:category},
			dataType : "json",
			success: function(data) {
				ret = data.msg === "OK";
			}
		});
	
		//当ret= false时,添加该项到字典项
		if(ret == false){
		insertDict(category, itemName);
		  }
		}
		
		return ret;
	}
	
	//插入操作
	insertDict = function(category, itemName){
		var ret = false;
		$.ajax({
			url: '/crm/dataDict/addDataDict', 
			type: "post",
			async : false,
			data : {itemName:itemName, category:category, isValid:1,itemSort:0},
			dataType : "json",
			success: function(data) {
				ret = data.msg === "OK";
			}
		});
		
		return ret;
	}
	otherIsExist = function(name,category ,itemName){
		    
			if(checkIfExist(category, itemName)){
			   
				alert(name+"已存在，请从下拉项中选择");
				
				window.location.reload();
			
				return true;
			}
			return false ;
		}
$(function(){
 
	 $('#disagreeType_add').blur( 
	         function() { 
	          checkIfExist("${disagreeType_add}" ,$('#disagreeType_add').val());
	         });  
	 $('#specialIssueType_add').blur( 
	         function() { 
	          checkIfExist("${specialIssueType_add}" ,$('#specialIssueType_add').val());
	         });  
				
	//多选设置
	 $(".multiSelect").select2({
		  placeholder: "请选择...",
	      tags: true,
	      tokenSeparators: [',', ' ']
	    })
	    
	      //审核报告-审核类型
     $('#report_auditType').change(function(){
			showCountersign($(this).val());
		});
	
	 //审核报告保存
      $("#insertReport").click(function(){
         
        //单选
		var enterpriceIsTure;
		if (!$("#enterpriceIsTure").is(':checked')) {
			enterpriceIsTure = 0;
		} else {
			enterpriceIsTure = 1;
		}
		var enterpriceRegisterPar;
		if (!$("#enterpriceRegisterPar").is(':checked')) {
			enterpriceRegisterPar = 0;
		} else {
			enterpriceRegisterPar = 1;
		}

		var enterpricePositionPar;
		if (!$("#enterpricePositionPar").is(':checked')) {
			enterpricePositionPar = 0;
		} else {
			enterpricePositionPar = 1;
		}
        var enterpriceTurnoverPar;
		if (!$("#enterpriceTurnoverPar").is(':checked')) {
			enterpriceTurnoverPar = 0;
		} else {
			enterpriceTurnoverPar = 1;
		}
		var enterpriceBlackMark;
		if (!$("#enterpriceBlackMark").is(':checked')) {
			enterpriceBlackMark = 0;
		} else {
			enterpriceBlackMark = 1;
		}

		var reterenceInfoPar;
		if (!$("#reterenceInfoPar").is(':checked')) {
			reterenceInfoPar = 0;
		} else {
			reterenceInfoPar = 1;
		}

		var reterenceKnow;
		if (!$("#reterenceKnow").is(':checked')) {
			reterenceKnow = 0;
		} else {
			reterenceKnow = 1;
		}
		//多选
        var materialShortType = $('#materialShortType').select2("val")== null ? '' : $('#materialShortType').val().toString();
        var nonefillField = $('#nonefillField').select2("val")== null ? '' : $('#nonefillField').val().toString();
        
        var disagreeType = $('#disagreeType').select2("val")== null ? '' : $('#disagreeType').val().toString();
        var specialIssueType = $('#specialIssueType').select2("val")== null ? '' : $('#specialIssueType').val().toString();
        
        //多选-补充其他
		if(otherIsExist("不符归类 其他分类","${disagreeType_add}" ,$('#disagreeType_add').val())){ return ;}
		if(otherIsExist("个别问题分类 其他问题分类","${specialIssueType_add}" ,$('#specialIssueType_add').val())){ return ;} 
        
        //评价
        var applyRemark = $('#applyRemark').val();
        var materialStatus = $('#materialStatus').val();
        var auditFstName = $('#auditFstName').val();
        var auditType = $('#report_auditType').val();
        var financeReportType = $('#financeReportType').val();
        var materialSndName = $('#materialSndName').val();
        var materialCompleteName = $('#materialCompleteName').val();
        var disagreeReason = $('#disagreeReason').val();
        var specialIssue = $('#specialIssue').val();
        var remarkCommon = $('#remarkCommon').val();
        var remarkIssueDetail = $('#remarkIssueDetail').val();
        var leadershipOpinion = $('#leadershipOpinion').val();
		var recommendation = $('#recommendation').val();
		
		
        //会签日期
		var countersignStartDate = $('#countersignStartDate').val();
		
		var signDate1 = $('#signDate1').val();
		var signDate2 = $('#signDate2').val();
		var signDate3 = $('#signDate3').val();
		var signDate4 = $('#signDate4').val();
		var signDate5 = $('#signDate5').val();
		var countersignEndDate = $('#countersignEndDate').val();
		
		var tbAuditReportId = $('#tbAuditReportId').val();
	   
	    //判断评价和推荐语非空
	    if(isBlank(recommendation,'推荐语','recommendationTips') && isBlank(applyRemark,'评价','applyRemarkTips')){
	      
	      $.ajax({
				url : '/crm/auditreport/insert',
				type : 'post',
				data : {
				    id : tbAuditReportId,
				    
					applyRemark : applyRemark,
					recommendation : recommendation,
					specialIssue : specialIssue,
					remarkCommon :remarkCommon,
					
					enterpriceIsTure : enterpriceIsTure,
					enterpriceRegisterPar : enterpriceRegisterPar,
					enterpriceTurnoverPar : enterpriceTurnoverPar,
					enterpriceBlackMark : enterpriceBlackMark,
					enterpricePositionPar : enterpricePositionPar,
					reterenceInfoPar : reterenceInfoPar,
					reterenceKnow : reterenceKnow,
					
					materialStatus : materialStatus,
					auditFstName : auditFstName,
					specialIssueType : specialIssueType,
					materialShortType : materialShortType,
					financeReportType : financeReportType,
					auditType : auditType,
					materialSndName : materialSndName,
					materialCompleteName : materialCompleteName,
					disagreeReason : disagreeReason,
				    disagreeType : disagreeType,
					nonefillField : nonefillField,
					remarkIssueDetail : remarkIssueDetail,
					
					countersignStartDateStr : countersignStartDate,
					signDate1Str : signDate1,
					signDate2Str : signDate2,
					signDate3Str : signDate3,
					signDate4Str : signDate4,
					signDate5Str : signDate5,
					countersignEndDateStr : countersignEndDate,	
					
					
					leadershipOpinion : leadershipOpinion
				},
				success : function(obj) {
				console.log(obj);
				if(obj.msg=="OK"){
				 $('#contractModal').modal('hide');
					greeting({
						msg : "操作成功!",
						confirm : function() {
							location.href = "/crm/secondaudit/secondAuditPage?uid=${vo.user_id}&flag=" + $('#flag').val();
						}
					});
				  }else{
				   alert(obj.msg);
				  }
				},
				error : function(obj) {
					console.log(obj.msg);
					alert(obj.msg);
				}
			});
	      
	    }
      });
    
	
	  $('.my_datepicker').datetimepicker({
        dayViewHeaderFormat: 'YYYY MMMM',
        locale: 'zh-cn',
        format: 'YYYY-MM-DD ',
        keepInvalid: true
    }); 
	  $('.my_timepicker').datetimepicker({
        dayViewHeaderFormat: 'YYYY MMMM',
        locale: 'zh-cn',
        format: 'YYYY-MM-DD HH:mm:ss ',
        keepInvalid: true
    }); 
  	//合同表
    $('#createTime').data('DateTimePicker').useCurrent(false);
   // $('#register_datetime').data('DateTimePicker').useCurrent(false);
   
    //首先将#back-to-top隐藏
    $("#back-to-top").hide();
    //当滚动条的位置处于距顶部1500像素以下时，跳转链接出现，否则消失
    $(window).scroll(function() {
        if ($(window).scrollTop() > 1400) {
            $("#back-to-top").fadeIn(500);
        } else {
            $("#back-to-top").fadeOut(500);
        }
    });
    //当点击跳转链接后，回到页面顶部位置
    $("#back-to-top").click(function() {
        $('body,html').animate({
            scrollTop: 0
        },
        500);
        return false;
    });
    
  
});
</script>